﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

using Telerik.Windows.Controls;

namespace Centrica.TaskManager.Task.Calendar
{
    public static class ProviderManager
    {

        /// <summary>
        /// Identifies the <see cref="ProviderProperty"/>.
        /// </summary>
        public static readonly DependencyProperty ProviderProperty =
            DependencyProperty.RegisterAttached(
                "Provider", typeof(ISchedulerProvider), typeof(ProviderManager), new PropertyMetadata(null, OnProviderChanged));

        /// <summary>
        /// Gets the provider.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <returns></returns>
        public static ISchedulerProvider GetProvider(DependencyObject obj)
        {
            return (ISchedulerProvider)obj.GetValue(ProviderProperty);
        }

        /// <summary>
        /// Sets the provider.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <param name="value">The value.</param>
        public static void SetProvider(DependencyObject obj, ISchedulerProvider value)
        {
            obj.SetValue(ProviderProperty, value);
        }

        private static void OnProviderChanged(DependencyObject target, DependencyPropertyChangedEventArgs changedEventArgs)
        {
            RadScheduler scheduler = target as RadScheduler;
            if (scheduler != null)
            {
                ISchedulerProvider oldSchedulerProvider = changedEventArgs.OldValue as ISchedulerProvider;
                if (oldSchedulerProvider != null)
                {
                    oldSchedulerProvider.Detach();
                }

                ISchedulerProvider newSchedulerProvider = changedEventArgs.NewValue as ISchedulerProvider;
                if (newSchedulerProvider != null)
                {
                    newSchedulerProvider.Attach(scheduler);
                }
            }
        }



    }
}
